package cn.jji8.PlayerLimitQuickShop;

import cn.jji8.PlayerLimitQuickShop.logger.Language;
import cn.jji8.PlayerLimitQuickShop.logger.Logger;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.maxgamer.quickshop.QuickShop;
import org.maxgamer.quickshop.event.ShopPurchaseEvent;
import org.maxgamer.quickshop.shop.Shop;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Listener implements org.bukkit.event.Listener,CommandExecutor,TabCompleter{
    Main main;
    Datas datas;
    QuickShop QuickShop;
    HashMap<Player,Shop> PlayerShopMap = new HashMap<>();

    /**
     * 构造器
     * */
    public Listener(Main main){
        QuickShop = (org.maxgamer.quickshop.QuickShop) org.bukkit.Bukkit.getPluginManager().getPlugin("QuickShop");
        this.main = main;
        datas=new Datas(new File(main.getDataFolder(),"Datas"));
    }
    /**
     * 保存插件全部数据
     * */
    public void saveAll(){
        datas.saveAll();
    }
    /**
     * 玩家购买商店物品时触发
     * */
    @EventHandler
    public void ShopPurchaseEvent(ShopPurchaseEvent a){
        Logger.putdebug(a.getPlayer()+"玩家尝试购出售物品");
        if (!datas.ifPlayerCanSellForShop(a.getShop().getLocation().toString(),a.getPlayer(),a.getAmount())){
            a.setCancelled(true);
            Logger.putdebug(a.getPlayer()+"拒绝购买");
        }
    }
    /***
     * 玩家与空气方块交互时
     */
    @EventHandler(ignoreCancelled = true)
    public void onClick(PlayerInteractEvent e) {
        Logger.putdebug(e.getPlayer()+"玩家交互");
        Block b = e.getClickedBlock();
        if (e.getAction() != Action.RIGHT_CLICK_BLOCK)
            return; // Didn't right click it, we dont care.//没有右键点击，我们不在乎。
        Logger.putdebug(e.getPlayer()+"玩家是右键");
        Shop shop = QuickShop.getShopManager().getShop(b.getLocation());
        if (shop == null)
            return;// If they're either survival or the owner, they can break it
        Logger.putdebug(e.getPlayer()+"与商店交互");
        PlayerShopMap.put(e.getPlayer(),shop);
        Logger.putdebug(e.getPlayer()+"选择商店成功");
        Logger.putPlayerChat(e.getPlayer(),Language.get("选择商店成功","选择商店成功"));
    }
    /**
     * 命令执行器
     * */
    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
        if(strings.length>=1){
            if(equalsOneIgnoreCase(strings[0], new String[]{"帮助", "help"})){
                List<String> help = new ArrayList<>();
                help.add("----------------插件帮助----------------");
                help.add("主命令：");
                help.add("“PlayerLimitQuickShop”或“plqs”或“商店限制”");
                help.add("参数1：");
                help.add("“reload”或“重新加载配置文件” -----> 见名知意awa");
                help.add("“setShopTiemPlayerSellMaxMoney”或“设置商店指定时间内玩家购买最大可获得钱数” -----> 见名知意awa");
                help.add("“setShopAllSellMaxMoney”或“设置商店指定时间内全部玩家购买最大可获得钱数” -----> 见名知意awa");
                help.add("“delete”或“删除限制” -----> 见名知意awa");
                help.add("---------------------------------------");
                help = Language.get("插件帮助",help);
                for(String he:help){
                    Logger.putPlayerChat(commandSender,he);
                }
                return true;
            }
            if(equalsOneIgnoreCase(strings[0], new String[]{"reload", "重新加载配置文件"})){
                if(!commandSender.hasPermission("PlayerLimitQuickShop.reload")){
                    Logger.putPlayerChat(commandSender,Language.get("reload无权限","你没有执行此命令的权限，需要：PlayerLimitQuickShop.reload"));
                }
                commandSender.sendMessage("重载完成");
                return true;
            }
            if(equalsOneIgnoreCase(strings[0],new String[]{"setShopTiemPlayerSellMaxMoney","设置商店指定时间内玩家购买最大可获得钱数"})){
                if(!commandSender.hasPermission("PlayerLimitQuickShop.setShopTiemPlayerSellMaxMoney")){
                    Logger.putPlayerChat(commandSender,Language.get("setShopTiemPlayerSellMaxMoney无权限","你没有执行此命令的权限，需要：PlayerLimitQuickShop.setShopTiemPlayerSellMaxMoney"));
                }
                if(strings.length<3){
                    Logger.putPlayerChat(commandSender,Language.get("setShopTiemPlayerSellMaxMoney提示","setShopTiemPlayerSellMaxMoney 时间(毫秒) 钱数"));
                    return true;
                }
                long time;
                double money;
                try {
                    time = Integer.valueOf(strings[1]);
                }catch (IllegalArgumentException a){
                    Map<String,Object> map = new HashMap<>();
                    map.put("%错误数字%",strings[1]);
                    Logger.putPlayerChat(commandSender,Language.get("错误数字提示","%错误数字%不是一个有效数字",map));return true;
                }
                try {
                    money = Double.valueOf(strings[2]);
                } catch (IllegalArgumentException a){
                    Map<String,Object> map = new HashMap<>();
                    map.put("%错误数字%",strings[2]);
                    Logger.putPlayerChat(commandSender,Language.get("错误数字提示","%错误数字%不是一个有效数字",map));return true;
                }
                Shop shop = PlayerShopMap.get(commandSender);
                if(shop==null){
                    Logger.putPlayerChat(commandSender,Language.get("未选择商店","你还没有选择商店，请选择商店后重试。(右键商店箱子可选择商店)"));
                    return true;
                }
                if(!shop.getOwner().equals(((Player)commandSender).getUniqueId())){
                    if(!commandSender.hasPermission("PlayerLimitQuickShop.others")){
                        Logger.putPlayerChat(commandSender,Language.get("没有权限设置别人的商店","你没有设置别人商店的权限，需要：PlayerLimitQuickShop.others"));
                    }
                }
                datas.setShopTiemPlayerSellMaxMoney(shop.getLocation().toString(),time,money);
                Logger.putPlayerChat(commandSender,Language.get("设置成功","设置成功"));
                return true;
            }
            if(equalsOneIgnoreCase(strings[0],new String[]{"setShopAllSellMaxMoney","设置商店指定时间内全部玩家购买最大可获得钱数"})){
                if(!commandSender.hasPermission("PlayerLimitQuickShop.setShopAllSellMaxMoney")){
                    Logger.putPlayerChat(commandSender,Language.get("setShopAllSellMaxMoney无权限","你没有执行此命令的权限，需要：PlayerLimitQuickShop.setShopAllSellMaxMoney"));
                }
                if(strings.length<3){
                    Logger.putPlayerChat(commandSender,Language.get("setShopAllSellMaxMoney提示","setShopAllSellMaxMoney 时间(毫秒) 钱数"));
                    return true;
                }
                long time;
                double money;
                try {
                    time = Integer.valueOf(strings[1]);
                }catch (IllegalArgumentException a){
                    Map<String,Object> map = new HashMap<>();
                    map.put("%错误数字%",strings[1]);
                    Logger.putPlayerChat(commandSender,Language.get("错误数字提示","%错误数字%不是一个有效数字",map));return true;
                }
                try {
                    money = Double.valueOf(strings[2]);
                } catch (IllegalArgumentException a){
                    Map<String,Object> map = new HashMap<>();
                    map.put("%错误数字%",strings[2]);
                    Logger.putPlayerChat(commandSender,Language.get("错误数字提示","%错误数字%不是一个有效数字",map));return true;
                }
                Shop shop = PlayerShopMap.get(commandSender);
                if(shop==null){
                    Logger.putPlayerChat(commandSender,Language.get("未选择商店","你还没有选择商店，请选择商店后重试。(右键商店箱子可选择商店)"));
                    return true;
                }
                if(!shop.getOwner().equals(((Player)commandSender).getUniqueId())){
                    if(!commandSender.hasPermission("PlayerLimitQuickShop.others")){
                        Logger.putPlayerChat(commandSender,Language.get("没有权限设置别人的商店","你没有设置别人商店的权限，需要：PlayerLimitQuickShop.others"));
                    }
                }
                datas.setShopAllSellMaxMoney(shop.getLocation().toString(),time,money);
                Logger.putPlayerChat(commandSender,Language.get("设置成功","设置成功"));
                return true;
            }
            if(equalsOneIgnoreCase(strings[0],new String[]{"delete","删除限制"})){
                if(!commandSender.hasPermission("PlayerLimitQuickShop.delete")){
                    Logger.putPlayerChat(commandSender,Language.get("delete无权限","你没有执行此命令的权限，需要：PlayerLimitQuickShop.delete"));
                }
                Shop shop = PlayerShopMap.get(commandSender);
                if(shop==null){
                    Logger.putPlayerChat(commandSender,Language.get("未选择商店","你还没有选择商店，请选择商店后重试。(右键商店箱子可选择商店)"));
                    return true;
                }
                if(!shop.getOwner().equals(((Player)commandSender).getUniqueId())){
                    if(!commandSender.hasPermission("PlayerLimitQuickShop.others")){
                        Logger.putPlayerChat(commandSender,Language.get("没有权限设置别人的商店","你没有设置别人商店的权限，需要：PlayerLimitQuickShop.others"));
                    }
                }
                datas.delete(shop.getLocation().toString());
                Logger.putPlayerChat(commandSender,Language.get("删除成功","删除成功"));
                return true;
            }
        }
        Logger.putPlayerChat(commandSender,Language.get("参数错误","参数错误"));
        return false;
    }
    /**
     * 命令补全器
     * */
    @Override
    public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) {
        Logger.putdebug("命令补全");
        for(String q:strings){
            Logger.putdebug(q);
        }
        if(strings.length>1){
            if(equalsOneIgnoreCase(strings[0],new String[]{"setShopTiemPlayerSellMaxMoney","设置商店指定时间内玩家购买最大可获得钱数","setShopAllSellMaxMoney","设置商店指定时间内全部玩家购买最大可获得钱数"})){
                if(strings.length==3){
                    return setShopTiemPlayerSellMaxMoney2;
                }
                return setShopTiemPlayerSellMaxMoney;
            }
            return null;
        }
        return no1CommendList;
    }
    static ArrayList<String> no1CommendList = new Object(){
        public ArrayList<String> getCommendList() {
            ArrayList<String> list = new ArrayList<>();
            list.add("reload");
            list.add("重新加载配置文件");
            list.add("setShopTiemPlayerSellMaxMoney");
            list.add("设置商店指定时间内玩家购买最大可获得钱数");
            list.add("setShopAllSellMaxMoney");
            list.add("设置商店指定时间内全部玩家购买最大可获得钱数");
            list.add("delete");
            list.add("删除限制");
            return list;
        }
    }.getCommendList();
    static ArrayList<String> setShopTiemPlayerSellMaxMoney = new Object(){
        public ArrayList<String> getCommendList() {
            ArrayList<String> list = new ArrayList<>();
            list.add("时间（单位：毫秒）");
            return list;
        }
    }.getCommendList();
    static ArrayList<String> setShopTiemPlayerSellMaxMoney2 = new Object(){
        public ArrayList<String> getCommendList() {
            ArrayList<String> list = new ArrayList<>();
            list.add("钱数");
            return list;
        }
    }.getCommendList();
    /**
     * 比较器，只要字符与字符列表的其中一个相同就返回true
     * */
    boolean equalsOneIgnoreCase(String s,String[] strings){
        for(String s1:strings){
            if(s.equalsIgnoreCase(s1)){
                return true;
            }
        }
        return false;
    }
}
